package tt.dz.biz.service.smssend;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import tt.dz.common.DzDigester;
import tt.dz.common.LogUtil;

@Service("DZSMSMessage")
@Scope("prototype")
public class DZSMSMessage
  implements SendAbleConfigMessageI
{
  private static String SMSMESSAGEURL;
  private static String CDKEY;
  private static String PASSWORD;
  private boolean isSend;
  
  static
  {
    Properties prop = new Properties();
    InputStream in = null;
    try
    {
      in = DZSMSMessage.class.getClassLoader().getResourceAsStream("ms-context.properties");
      prop.load(in);
      
      SMSMESSAGEURL = prop.getProperty("SMSMessage.url").trim();
      CDKEY = prop.getProperty("SMSMessage.cdKey");
      PASSWORD = DzDigester.decryptCommonInfo(prop.getProperty("SMSMessage.password")); ;
    }
    catch (Exception e)
    {
      LogUtil.error("加载短信发送资源信息异常", e);
    }
    finally
    {
      if (in != null) {
        try
        {
          in.close();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }
    }
  }
  
  public String sendMessage(String phone, String message)
    throws Exception
  {
    if (!this.isSend) {
      return "";
    }
    Map<String, String> map = new HashMap();
    map.put("apikey", CDKEY);
//    map.put("password", PASSWORD);
    map.put("mobile", phone);
    map.put("content", message);
    String xml = SendSMSHelpHandler.post(SMSMESSAGEURL, map, "UTF-8");
    return xml;
  }
  
  public void isAbleSend(boolean isSend)
  {
    this.isSend = isSend;
  }
  public static void main(String[] args) throws Exception {
	  Map<String, String> map = new HashMap();
	    map.put("apikey", "b10004fb3773d29b3de8b3a9d53f3bf7");
//	    map.put("password", PASSWORD);
	    map.put("mobile", "18010511232");
	    
	    
	    map.put("content", "【电桩】忘记密码修改验证码为:308429,验证码有效期为5分钟");
	    String xml = SendSMSHelpHandler.post(SMSMESSAGEURL, map, "UTF-8");
	  System.out.println(xml);
}
}
